package com.biovr.backend.restcontroller;

import com.biovr.backend.domain.Apparatus;
import com.biovr.backend.domain.PublishMessage;
import com.biovr.backend.repository.PublishMessageReposiory;
import com.biovr.backend.utils.DateTimeUtils;
import com.biovr.backend.utils.ResponseFactory;
import com.biovr.backend.utils.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by monodev on 2017/6/7.
 * 服务端推送rest控制器
 */
@Api(value = "/serverpub",description = "服务器http推送", consumes="application/json")
@RestController
@RequestMapping("/serverpub")
public class PublishMessageRestController {
    @Autowired
    PublishMessageReposiory publishMessageReposiory;
    @Autowired
    DateTimeUtils dateTimeUtils;

    @GetMapping
    public RestResponse<List<PublishMessage>> getAll(){
        return ResponseFactory.createSuccessResult(publishMessageReposiory.findAll());
    }
    /**
     * 根据id查找
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value="根据id查找",response = PublishMessage.class)
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string",paramType = "path")
    public RestResponse<PublishMessage> getById(@PathVariable String id){
        PublishMessage publishMessage = publishMessageReposiory.findOne(id);
        return ResponseFactory.createRestResponse(true,"",publishMessage);
    }
    /**
     * 根据推送类型查找
     * @param publishTarget
     * @return
     */
    @ApiOperation(value="根据推送类型查找",notes = "推送类型：index-表示首页推送",response = PublishMessage.class,responseContainer = "List")
    @ApiImplicitParam(name = "publishTarget", value = "index", required = true, dataType = "string",paramType = "path")
    @GetMapping("/target/{publishTarget}")
    public RestResponse<List<PublishMessage>> getByPublishTarget(@PathVariable String publishTarget){
        List<PublishMessage> publishMessages = publishMessageReposiory.findByPublishTarget(publishTarget);
        return ResponseFactory.createRestResponse(true,"",publishMessages);
    }

    /**
     * 新增pub
     * @param publishMessage
     * @return
     */
    @ApiOperation(value="新增推送信息",notes = "json格式",response = PublishMessage.class)
    @ApiImplicitParam(name = "publishMessage", value = "新增pub对象，注意publishTarget字段", required = true, dataType = "PublishMessage")
    @PostMapping
    public RestResponse<PublishMessage> addSavePub(@RequestBody PublishMessage publishMessage) throws Exception{
        publishMessage.setPublishTime(dateTimeUtils.getCurrDateTimeString());
        PublishMessage pub  = publishMessageReposiory.insert(publishMessage);
        return ResponseFactory.createRestResponse(true,"",pub);
    }

    /**
     * 更新pub
     * @param id
     * @param publishMessage
     * @return
     * @throws Exception
     */
    @PutMapping("/{id}")
    public RestResponse<PublishMessage> putPub(@PathVariable String id,@RequestBody PublishMessage publishMessage) throws Exception{
        publishMessage.setId(id);
        return ResponseFactory.createSuccessResult(publishMessageReposiory.save(publishMessage)) ;
    }

    /**
     * 删除pub
     * @param id
     * @return
     * @throws Exception
     */
    @DeleteMapping("/{id}")
    public RestResponse<?> deletePub(@PathVariable String id) throws Exception{
      PublishMessage msg = publishMessageReposiory.findOne(id);
        if(msg!=null){
            publishMessageReposiory.delete(msg);
            return ResponseFactory.createSuccessResult();
        }
      else{
          return  ResponseFactory.createErrorResult("消息不存在");
        }
    }
}
